#ifndef _RS485_H
#define _RS485_H

#include "Serial.h"
#include "gpio.h"
#include "msg_def.h"
#include "pthread.h"
#include "wontec_485.h"


#define RTS_IO GPIO6
#define RS485_TTYS_NUM 6 

//#define RTS_IO GPIO14
//#define RS485_TTYS_NUM 3

//#define WONTEC_MAX_DEVID 6

typedef int (* rs485_cb)(void * stdData, void * recvData);

class CRs485 : public CSerial
{
private:
    int m_idQueueMsg;
    unsigned int m_rts_pin;
    CGpio * m_gpioHandle;
    pthread_t m_msg_thread_handle;

    rs485_cb m_completeCbFunc[WONTEC_MAX_DEVID];

    wontec_485_frame * m_completeCbData[WONTEC_MAX_DEVID];

    int m_status[WONTEC_MAX_DEVID];

    pthread_mutex_t m_mutexCheckRecv;
public:
    CRs485();
    virtual ~CRs485();
    int openPort(int portNum, unsigned int pinRTS,serialConfig * ptrSerialConfig);
    void closePort(void); 

    void setQueueMsgId(int keyIn);
    int getQueueMsgId(void);
    
    int send(unsigned char * buff, int len); 

    virtual int PackagePro( char* Buf, int len );  

    void cmdAct(void * dataIn);

    void setCompleteCb(int devId,rs485_cb ptrFuncIn,wontec_485_frame * expectedData);
    
    int getCurrStatus(int devId);

    void setCurrStatus(int devId, int statusIn);

    void sendAckToGrpc(int statusCodeIn,int errorNumIn,int sid, int moduleId);

    void checkRecvFrame(wontec_485_frame * dataRecv);
     
};

void* ReceiveMsgFunc(void* lparam);


#endif
